package com.itheima.sfbx.insurance.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.sfbx.framework.commons.basic.ResponseResult;
import com.itheima.sfbx.framework.commons.utils.ResponseResultBuild;
import com.itheima.sfbx.insurance.dto.ConditionVO;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "保障管理")
@RestController
@RequestMapping("safeguard")
public class SafeguardController {
    @Autowired
    private ISafeguardService safeguardService;

    /**
     * 分页查询
     * @param safeguardVO 查询条件
     * @param pageNum 页号
     * @param pageSize 页大小
     * @return 查询结果
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "根据条件分页査询保障项\"，",notes = "根据条件分页査询保障项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "safeguardVO",value = "查询条件VO对象",required = true,dataType = "safeguardVO"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"safeguardVO.safeguardType\",\"safeguardVO.safeguardKey\",\"safeguardVO.safeguardVal\",\"safeguardVO.remake"})
    public ResponseResult<Page<SafeguardVO>> findConditionVOPage(
            @RequestBody SafeguardVO safeguardVO,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        log.warn("查询条件VO对象：{}",safeguardVO);
        Page<SafeguardVO> safeguardVOVOPage = safeguardService.findPage(safeguardVO, pageNum, pageSize);
        return ResponseResultBuild.successBuild(safeguardVOVOPage);
    }

    /**
     * 新增
     * @param safeguardVO 新增的保障项VO对象
     * @return 新增的保障项VO对象
     */

    @ApiOperation(value = "新增保障项",notes = "新增保障项")
    @PutMapping
    @ApiOperationSupport(includeParameters = {"safeguardVO.safeguardType\",\"safeguardVO.safeguardKey\",\"safeguardVO.safeguardVal\",\"safeguardVO.remake"})
    public ResponseResult<SafeguardVO> createSafeguard(@RequestBody SafeguardVO safeguardVO) {
        log.warn("新增的保障项VO对象：{}",safeguardVO);
        SafeguardVO safeguardVOResult = safeguardService.save(safeguardVO);
        return ResponseResultBuild.successBuild(safeguardVOResult);
    }
    /**
     * 修改
     * @param safeguardVO 修改的保障项VO对象
     * @return 修改的保障项VO对象
     */
    @PatchMapping
    @ApiOperation(value = "保存编辑保障项",notes = "保存编辑保障项")
    @ApiOperationSupport(includeParameters = {"safeguardVO.safeguardType\",\"safeguardVO.safeguardKey\",\"safeguardVO.safeguardVal\",\"safeguardVO.remake"})
    public ResponseResult<Boolean> updateSafeguard(@RequestBody SafeguardVO safeguardVO) {
        log.warn("修改的保障项VO对象：{}",safeguardVO);
        Boolean update = safeguardService.update(safeguardVO);
        //判断是否修改成功
        if (!update){
         throw new RuntimeException("修改失败");
        }
        return ResponseResultBuild.successBuild(update);
    }

    @ApiOperation(value = "删除保障项",notes = "删除保障项")
    @DeleteMapping
    public ResponseResult<Boolean> deleteSafeguard(@RequestBody SafeguardVO safeguardVO) {
        log.warn("删除的保障项ID数组：{}",safeguardVO);
        Boolean delete = safeguardService.delete(safeguardVO.getCheckedIds());
        //判断是否删除成功
        if (!delete){
            throw new RuntimeException("删除失败");
        }
        return ResponseResultBuild.successBuild(delete);
    }

    /***
     * 多条件查询保障项列表
     * @param safeguardVO 保障项VO对象
     * @return List<SafeguardVO>
     */
    @PostMapping("/list")
    @ApiOperation(value = "多条件查询保障项列表",notes = "多条件查询保障项列表")
    @ApiImplicitParam(name = "safeguardVO",value = "保障项VO对象",required = true,dataType = "SafeguardVO")
    @ApiOperationSupport(includeParameters = {"conditionVO.dataState","conditionVO.conditionKey","conditionVO.conditionKeyName","conditionVO.conditionVal","conditionVO.sortNo","conditionVO.remake"})
    public ResponseResult<List<SafeguardVO>> findList(@RequestBody SafeguardVO safeguardVO) {
        log.warn("多条件查询保障项列表：{}",safeguardVO);
        List<SafeguardVO> list = safeguardService.findList(safeguardVO);
        return ResponseResultBuild.successBuild(list);
    }
}

